home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 6580 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.2 KB  |  65 lines

  1. Path: dawn.mmm.com!news
  2. From: kjhopps@mmm.com (Kevin J Hopps)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Floating Point: Overflow ?
  5. Date: 9 Feb 1996 19:04:09 GMT
  6. Organization: 3M - St. Paul, MN  55144-1000 US
  7. Message-ID: <4fg5r9$bqm@dawn.mmm.com>
  8. References: <311927bc.318595@198.112.179.16>
  9. Reply-To: kjhopps@mmm.com
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. confused (confused@c++.oop) wrote:
  13. > I am confused and a bit embarassed by this problem! I really should
  14. > know the answer but ...
  15.  
  16. > I am using BC++ 4.52 and am doing a review of C++ using the text 
  17. > "Object-Oriented Programming in C++" by Robert Lafore. One exercise is
  18. > giving me a floating point overflow error. Basically the user input is
  19. > a number of gallons of water and the output is the number of cubic
  20. > feet the water occupies.
  21.  
  22. > #include<iostream.h>
  23.  
  24. > void main()
  25. > {
  26. >     float gals;
  27. >     cin << gals;
  28. >     cout >> endl >> gals/7.481;
  29. > }
  30.  
  31. When I try to compile this, I get:
  32. "x.cc", line 4: Warning (Anachronism): main() must have a return type of int.
  33. "x.cc", line 4: Note: Type "CC -migration" for more on anachronisms.
  34. "x.cc", line 6: Error: The operation "istream_withassign << float" is illegal.
  35. "x.cc", line 7: Error: Trying to take the address of the overloaded function endl.
  36. "x.cc", line 7: Error: The operation "ostream_withassign >> int" is illegal.
  37.  
  38. I'm surprised that your compiler did not complain.  The following program works
  39. much better.  Notice that the direction of the "arrows" on cin and cout are
  40. reversed.
  41.  
  42. #include <iostream.h>
  43.  
  44. int main()
  45. {
  46.     float gals;
  47.     cin >> gals;
  48.     cout << gals/7.481 << endl;
  49.     return 0;
  50. }
  51.  
  52. > When the input is an integer value (ie 5) there is no problem but when
  53. > the input is a decimal value (ie 5.5) I receive the overflow error.
  54. > Why am a receiving the error and how do I avoid this in the future?
  55.  
  56. When I enter 5.5 the program prints out 0.735196.
  57. --
  58. Kevin J. Hopps                  e-mail: kjhopps@mmm.com
  59. 3M Company                      phone:  (612) 737-4643
  60. 3M Center, Bldg. 235-2D-57      fax:    (612) 737-2700
  61. St. Paul, MN 55144-1000         Opinions are my own.  I don't speak for 3M.
  62.     But 3M speaks for me -- I did not write the following line:
  63.  
  64. Opinions expressed herein are my own and may not represent those of 3M.
  65.